def on_payment_verified(trade_order):
    pass


import json

from ..models.order import TradeOrder
from ..schema.resp import trade


def on_trade_finished(trade_order):
    data, _ = trade.dump(trade_order).data

    if trade_order.notify_info:
        notify_info = json.loads(trade_order.notify_info)
        url = notify_info.get('url', None)

        if url:
            _notify_trade_result_by_request(notify_info, trade_order)

    import datetime
    TradeOrder.query.filter(TradeOrder.id == data['id']).update(notify_status='done',
                                                                notify_time=datetime.datetime.now())


def _notify_trade_result_by_request(notify_info, trade_order):
    method = notify_info.get('method', None)
    url = notify_info.get('url', None)
    import requests
    if not method:
        method = 'GET'

    method = method.upper()

    response = None
    if method == 'GET':
        response = requests.get(url, params=trade_order)
    if method == 'POST':
        response = requests.post(url, data=trade_order, headers={'Content-Type: application/json'})

    if not response or not response.status_code == 200:
        raise
